package com.xunker.appointment.controller.api;

import com.xunker.appointment.exception.AppointmentException;
import com.xunker.appointment.model.hospital.Department;
import com.xunker.appointment.model.hospital.Hospital;
import com.xunker.appointment.model.schedule.Schedule;
import com.xunker.appointment.result.Result;
import com.xunker.appointment.result.ResultCodeEnum;
import com.xunker.appointment.service.DepartmentService;
import com.xunker.appointment.service.HospitalService;
import com.xunker.appointment.service.HospitalSetService;
import com.xunker.appointment.service.ScheduleService;
import com.xunker.appointment.utils.HttpRequestHelper;
import com.xunker.appointment.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/api/hospital")
public class ApiController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    //上传医院jiekou
    @PostMapping("saveHospital")
    public void saveHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        String sign = (String) objectMap.get("sign");
        String logoData = (String) objectMap.get("logoData");//图片字节码处理
        logoData=logoData.replaceAll(" ","+");
        objectMap.put("logoData",logoData);
        String signKey=hospitalSetService.getSignKey(hospitalCode);//签名校验
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            hospitalService.save(objectMap);
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    //查询医院
    @PostMapping("show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        String sign = (String) objectMap.get("sign");

        String signKey=hospitalSetService.getSignKey(hospitalCode);//签名校验
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            Hospital hospital=hospitalService.getByHospitalCode(hospitalCode);
            return Result.ok(hospital);
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    //上传部门接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        String sign = (String) objectMap.get("sign");
        String signKey=hospitalSetService.getSignKey(hospitalCode);
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            departmentService.save(objectMap);
            return Result.ok();
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    //查询所有部门信息
    @PostMapping("department/list")
    public Result getDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        int page= StringUtils.isEmpty(objectMap.get("page"))?1:Integer.parseInt((String) objectMap.get("page"));
        int limit= StringUtils.isEmpty(objectMap.get("limit"))?1:Integer.parseInt((String) objectMap.get("limit"));
        String sign = (String) objectMap.get("sign");

        String signKey=hospitalSetService.getSignKey(hospitalCode);//签名校验
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            Page<Department> departments=departmentService.getByHospitalCode(hospitalCode,page,limit);
            return Result.ok(departments);
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    //删除部门通过departmentCode
    @PostMapping("department/remove")
    public Result remove(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        String departmentCode = (String) objectMap.get("departmentCode");
        String sign = (String) objectMap.get("sign");
        String signKey=hospitalSetService.getSignKey(hospitalCode);
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            departmentService.remove(departmentCode);
            return Result.ok();
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        String sign = (String) objectMap.get("sign");
        String signKey=hospitalSetService.getSignKey(hospitalCode);
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            scheduleService.save(objectMap);
            return Result.ok();
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    //查询所有排班信息
    @PostMapping("schedule/list")
    public Result getSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        int page= StringUtils.isEmpty(objectMap.get("page"))?1:Integer.parseInt((String) objectMap.get("page"));
        int limit= StringUtils.isEmpty(objectMap.get("limit"))?1:Integer.parseInt((String) objectMap.get("limit"));
        String sign = (String) objectMap.get("sign");

        String signKey=hospitalSetService.getSignKey(hospitalCode);//签名校验
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            Page<Schedule> schedules=scheduleService.getByHospitalCode(hospitalCode,page,limit);
            return Result.ok(schedules);
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    //删除排班通过hosScheduleId
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        final Map<String, String[]> resquetMap = request.getParameterMap();
        final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
        String hospitalCode = (String) objectMap.get("hospitalCode");
        String id = (String) objectMap.get("id");
        String sign = (String) objectMap.get("sign");
        String signKey=hospitalSetService.getSignKey(hospitalCode);
        final String encrypt = MD5.encrypt(signKey);
        //调用service
        if(sign!=null&&signKey!=null&&sign.equals(encrypt)){
            scheduleService.remove(id);
            return Result.ok();
        }else {
            throw  new AppointmentException(ResultCodeEnum.SIGN_ERROR);
        }
    }

}
